package com.ruoyi.device.service.impl;

import java.util.List;

import com.ruoyi.common.constant.DeviceConstant;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.device.domain.SysDeviceNew;
import com.ruoyi.device.mapper.SysDeviceNewMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.device.mapper.SysDeviceBorrowMapper;
import com.ruoyi.device.domain.SysDeviceBorrow;
import com.ruoyi.device.service.ISysDeviceBorrowService;

/**
 * 设备借用Service业务层处理
 *
 * @author zz
 * @date 2024-04-24
 */
@Service
public class SysDeviceBorrowServiceImpl implements ISysDeviceBorrowService {
    @Autowired
    private SysDeviceBorrowMapper sysDeviceBorrowMapper;
    @Autowired
    private SysDeviceNewMapper sysDeviceNewMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询设备借用
     *
     * @param borrowId 设备借用主键
     * @return 设备借用
     */
    @Override
    public SysDeviceBorrow selectSysDeviceBorrowByBorrowId(Long borrowId) {
        SysDeviceBorrow sysDeviceBorrow = sysDeviceBorrowMapper.selectSysDeviceBorrowByBorrowId(borrowId);
//        SysDeviceNew sysDeviceNew = sysDeviceNewMapper.selectSysDeviceNewByDeviceId(sysDeviceBorrow.getDeviceId());
//        SysUser user = sysUserMapper.selectUserById(sysDeviceBorrow.getUserId());
//        sysDeviceBorrow.setSysDeviceNew(sysDeviceNew);
//        sysDeviceBorrow.setSysUser(user);
//        sysDeviceBorrow.setDeviceStatus(sysDeviceNew.getDeviceStatus());
        return sysDeviceBorrow;
    }

    /**
     * 查询设备借用列表
     *
     * @param sysDeviceBorrow 设备借用
     * @return 设备借用
     */
    @Override
    public List<SysDeviceBorrow> selectSysDeviceBorrowList(SysDeviceBorrow sysDeviceBorrow) {
        List<SysDeviceBorrow> sysDeviceBorrowList = sysDeviceBorrowMapper.selectSysDeviceBorrowList(sysDeviceBorrow);

        for (SysDeviceBorrow deviceBorrow : sysDeviceBorrowList) {
            Integer returnStatus = deviceBorrow.getReturnStatus();
            if(returnStatus.equals(0)){
                deviceBorrow.setReturnStr("申请借用");
            }else if(returnStatus.equals(1)){
                deviceBorrow.setReturnStr("未归还");
            }else if(returnStatus.equals(2)){
                deviceBorrow.setReturnStr("申请归还");
            }else{
                deviceBorrow.setReturnStr("已归还");
            }
        }
        return sysDeviceBorrowList;
    }

    /**
     * 新增设备借用
     *
     * @param sysDeviceBorrow 设备借用
     * @return 结果
     */
    @Override
    public int insertSysDeviceBorrow(SysDeviceBorrow sysDeviceBorrow) {
        //填充借用实体
        String userId = SecurityUtils.getUserId().toString();
        sysDeviceBorrow.setCreateBy(userId);
        sysDeviceBorrow.setUpdateBy(userId);
        sysDeviceBorrow.setUserId(SecurityUtils.getUserId());
        sysDeviceBorrow.setReturnRemark("暂未归还");
        sysDeviceBorrow.setReturnStatus(1);
        sysDeviceBorrow.setBorrowTime(DateUtils.getNowDate());
        sysDeviceBorrow.setCreateTime(DateUtils.getNowDate());
        sysDeviceBorrow.setUpdateTime(DateUtils.getNowDate());

        //修改设备信息的借用状态
        SysDeviceNew sysDeviceNew = new SysDeviceNew();
        sysDeviceNew.setDeviceId(sysDeviceBorrow.getDeviceId());
        sysDeviceNew.setDeviceStatus(1);
        sysDeviceNewMapper.updateSysDeviceNew(sysDeviceNew);

        return sysDeviceBorrowMapper.insertSysDeviceBorrow(sysDeviceBorrow);
    }

    /**
     * 新增设备借用
     *
     * @param sysDeviceBorrow 设备借用
     * @return 结果
     */
    @Override
    public int insertWeixSysDeviceBorrow(SysDeviceBorrow sysDeviceBorrow) {
        //填充借用实体
        String userId = SecurityUtils.getUserId().toString();
        sysDeviceBorrow.setCreateBy(userId);
        sysDeviceBorrow.setUpdateBy(userId);
        sysDeviceBorrow.setUserId(SecurityUtils.getUserId());
        sysDeviceBorrow.setReturnRemark("申请借用");
        sysDeviceBorrow.setReturnStatus(0);
        sysDeviceBorrow.setBorrowTime(DateUtils.getNowDate());
        sysDeviceBorrow.setCreateTime(DateUtils.getNowDate());
        sysDeviceBorrow.setUpdateTime(DateUtils.getNowDate());

        return sysDeviceBorrowMapper.insertSysDeviceBorrow(sysDeviceBorrow);
    }

    /**
     * 修改设备借用
     *
     * @param sysDeviceBorrow 设备借用
     * @return 结果
     */
    @Override
    public int updateSysDeviceBorrow(SysDeviceBorrow sysDeviceBorrow) {
        Integer deviceStatus = sysDeviceBorrow.getDeviceStatus();
        SysDeviceNew sysDeviceNew = new SysDeviceNew();
        sysDeviceNew.setDeviceStatus(deviceStatus);
        sysDeviceNew.setDeviceId(sysDeviceBorrow.getDeviceId());
        sysDeviceNewMapper.updateSysDeviceNew(sysDeviceNew);

        sysDeviceBorrow.setUpdateTime(DateUtils.getNowDate());
        String userId = SecurityUtils.getUserId().toString();
        sysDeviceBorrow.setUpdateBy(userId);
        //根据设备修改后的状态来判断归还状态
        //设备状态0是正常，2是维修，都认为归还了
        if(deviceStatus.equals(DeviceConstant.DEVICE_STATUS_NORMAL) ||
                deviceStatus.equals(DeviceConstant.DEVICE_STATUS_REPAIRS)){
            //已归还
            sysDeviceBorrow.setReturnStatus(DeviceConstant.DEVICE_BORROW_STATUS_RETURN);
        }else{
            //归还状态为 1，认为是未归还
            sysDeviceBorrow.setReturnStatus(DeviceConstant.DEVICE_BORROW_STATUS_LEND);
        }
        return sysDeviceBorrowMapper.updateSysDeviceBorrow(sysDeviceBorrow);
    }

    @Override
    public int updateSysDeviceBorrowWeixin(SysDeviceBorrow sysDeviceBorrow) {
        return sysDeviceBorrowMapper.updateSysDeviceBorrow(sysDeviceBorrow);
    }

    /**
     * 批量删除设备借用
     *
     * @param borrowIds 需要删除的设备借用主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceBorrowByBorrowIds(Long[] borrowIds) {
        return sysDeviceBorrowMapper.deleteSysDeviceBorrowByBorrowIds(borrowIds);
    }

    /**
     * 删除设备借用信息
     *
     * @param borrowId 设备借用主键
     * @return 结果
     */
    @Override
    public int deleteSysDeviceBorrowByBorrowId(Long borrowId) {
        return sysDeviceBorrowMapper.deleteSysDeviceBorrowByBorrowId(borrowId);
    }
}
